package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.Member;
import com.addplus.townmall.server.api.model.town.expand.MemberInformationExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商城会员表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface MemberMapper extends BaseAddMapper<Member> {


    /**
     * 方法描述：获取个人中心对应积分、我的资产
     *
     * @param
     * @return
     * @throws Exception
     * @author zhangjiehang
     * @date 2018/9/18 下午3:25
     */
    @Select("SELECT\n" +
            "\tmi.integral_now,\n" +
            "\tdm.balance\n" +
            "FROM\n" +
            "\tmember m\n" +
            "\tLEFT JOIN member_integral mi ON mi.member_id = m.id\n" +
            "\tLEFT JOIN distribution_member dm ON dm.member_id = m.id \n" +
            "\tAND dm.is_delete = 0\n" +
            "WHERE\n" +
            "\tm.id = #{member_id} AND m.is_deleted = 0")
    MemberInformationExpand getMemberInformationMiAndDmCount(@Param("member_id") Integer memberId);


    /**
     * 方法描述：获取个人中心(优惠券、账户余额、礼品卡）
     *
     * @param
     * @return
     * @throws Exception
     * @author zengqiwen
     * @date 2018/9/18 下午3:25
     */
    @Select("SELECT\n" +
            "\tcount(gmc.id) as gift_card_count\n" +
            "FROM\n" +
            "\tmember m\n" +
            "\tLEFT JOIN gift_member_card gmc ON gmc.member_id = m.id AND gmc.is_delete = 0 AND gmc.gift_status = 0\n" +
            "WHERE\n" +
            "\tm.id = #{member_id} AND m.is_deleted = 0")
    Integer getMemberInformationGifCount(@Param("member_id") Integer memberId);


    @Select("SELECT\n" +
            "\tcount(cmr.id) as coupon_count\n" +
            "FROM\n" +
            "\tmember m\n" +
            "\tLEFT JOIN coupon_member_record cmr ON cmr.member_id = m.id AND cmr.is_use = 0 AND cmr.is_delete = 0\n" +
            "WHERE\n" +
            "\tm.id = #{member_id} AND m.is_deleted = 0")
    Integer getMemberInformationCouponCount(@Param("member_id") Integer memberId);


    @Select("SELECT id,nick_name FROM member WHERE id = #{id} AND is_deleted = 0")
    Member getMemberBaseMessage(@Param("id") Integer id);


    @Select("select CONCAT_WS(':',a.invite_member_id,b.invite_member_id) as invite_member_id from member a left join member b on a.invite_member_id=b.id\n" +
            "    where a.id=#{id}")
    String getMemberInvite(@Param("id") Integer id);

    @Select(" select * from member  a where (a.cell_phone=#{phone}  or a.account=#{phone}) and a.is_deleted=0 ")
    Member getMemberLogin(@Param("phone")String phone);


    @Select("select * from member  where invite_member_id = #{memberId} and is_deleted=0  order by gmt_modified desc")
    List<MemberInformationExpand> getMyFriend(Page page, @Param("memberId")Integer memberId);


}
